
/*
 * Copyright (C) 2010 Bitglue. All rights reserved.
 *
 * $Id: XmlTreeUtils.java bself $
 */

package com.bitglue.web.admin.util;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * XmlTreeUtils for ease of build xml tree data.
 * @author Bryan Patrick Self
 */
public abstract class XmlTreeUtils {

    private static final String LEAF0 = "book_title1.gif";
    private static final String LEAF1 = "book.gif";
    private static final String LEAF2 = "book_title1.gif";

    private static final String FOLDER0 = "folderClosed.gif";
    private static final String FOLDER1 = "folderOpen.gif";
    private static final String FOLDER2 = "folderClosed.gif";

    private XmlTreeUtils() {
    }

    public static Element newTree() throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Element root = doc.createElement("tree");
        root.setAttribute("id", "0");
        return (Element)doc.appendChild(root);
    }

    public static Element newLeaf(Element folder, int id, String text) {
        Document doc = folder.getOwnerDocument();
        Element item = doc.createElement("item");
        item.setAttribute("text", text);
        item.setAttribute("id", String.valueOf(id));
        item.setAttribute("im0", LEAF0);
        item.setAttribute("im1", LEAF1);
        item.setAttribute("im2", LEAF2);
        return (Element)folder.appendChild(item);
    }

    public static Element newFolder(Element parent, int id, String text) {
        Document doc = parent.getOwnerDocument();
        Element item = doc.createElement("item");
        item.setAttribute("text", text);
        item.setAttribute("id", String.valueOf(id));
        item.setAttribute("im0", FOLDER0);
        item.setAttribute("im1", FOLDER1);
        item.setAttribute("im2", FOLDER2);
        return (Element)parent.appendChild(item);
    }

}
